﻿using Scaffold;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GridCut
{
	public partial class Manual : Form
	{
		public PCIBoard pci;

		public Manual()
		{
			InitializeComponent();
			btnXL.Tag = togXL.Tag = new object[] { PCIBoard.AXIS.X, -1 };
			btnXR.Tag = togXR.Tag = new object[] { PCIBoard.AXIS.X, 1 };
			btnYL.Tag = togYL.Tag = new object[] { PCIBoard.AXIS.Y, -1 };
			btnYR.Tag = togYR.Tag = new object[] { PCIBoard.AXIS.Y, 1 };

			btnHT.Tag = btnHTSeri.Tag = (ushort)2;
			btnHD.Tag = btnHDSeri.Tag = (ushort)3;
			btnTransL.Tag = (ushort)6;
			btnTransR.Tag = (ushort)7;
			pci = Form1.pci;
		}
		private void btnXY_MouseDown(object sender, MouseEventArgs e)
		{
			var axis = (PCIBoard.AXIS)((object[])(sender as Control).Tag)[0];
			var dir = (int)((object[])(sender as Control).Tag)[1];
			pci.KeepMove(axis, new PCIBoard.Speed(dir * Config.config.EVMin, dir * Config.config.EVMax, Config.config.ETacc));
		}
		private void btnXY_MouseUp(object sender, MouseEventArgs e)
		{
			pci.Stop();
		}
		private void togXY_CheckedChanged(object sender, EventArgs e)
		{
			if ((sender as CheckBox).Checked)
				btnXY_MouseDown(sender, null);
			else
				btnXY_MouseUp(sender, null);
		}


		private void btnH_MouseDown(object sender, MouseEventArgs e)
		{
			pci.Output((ushort)(sender as Control).Tag, true);
			pci.Output(5, true);        //同步打开油泵
		}
		private void btnH_MouseUp(object sender, MouseEventArgs e)
		{
			new Task(() =>
			{
				pci.Output((ushort)(sender as Control).Tag, false);
				Thread.Sleep(1000); pci.Output(5, false);   //延时1秒关闭油泵
			}).Start();

		}
		private void btnHSeri_Click(object sender, EventArgs e)
		{
			new Task(() =>
			{
				pci.Output((ushort)(sender as Control).Tag, true); pci.Output(5, true);     //同步打开油泵
				pci.WaitForInputState((ushort)(sender as Control).Tag, true);
				pci.Output((ushort)(sender as Control).Tag, false);
				Thread.Sleep(1000); pci.Output(5, false);   //延时1秒关闭油泵
			}).Start();
		}



		private void btnBumpOn_Click(object sender, EventArgs e)
		{
			pci.Output(5, true);
		}

		private void btnBumpOff_Click(object sender, EventArgs e)
		{
			pci.Output(5, false);
		}

		private void btnClkws_Click(object sender, EventArgs e)
		{
			new Task(() =>
			{
				if (pci.Read(2) != 0) return;   //输入口返回0表示有信号
				pci.Output(4, true); pci.Output(5, true);       //同步打开油泵
				pci.WaitForInputState(4, true);
				pci.Output(4, false);
				Thread.Sleep(1000); pci.Output(5, false);   //延时1秒关闭油泵
			}).Start();
		}

		private void btnAntClkws_Click(object sender, EventArgs e)
		{
			new Task(() => {
				if (pci.Read(2) != 0) return;   //输入口返回0表示有信号
				pci.Output(0, true); pci.Output(5, true);       //同步打开油泵
				pci.WaitForInputState(0, true);
				pci.Output(0, false);
				Thread.Sleep(1000); pci.Output(5, false);   //延时1秒关闭油泵
			}).Start();
		}

		private void btnKnifeOn_Click(object sender, EventArgs e)
		{
			pci.Output(1, true);
		}

		private void btnKnifeOff_Click(object sender, EventArgs e)
		{
			pci.Output(1, false);
		}
	}
}
